package com.yjz.jvm.juc.线程.线程算法题1;

public class Target {
    private int count;

    public synchronized void increase() {
        if (count == 2) {
            try {
                wait();  //当前线程进入this对象等待池
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count++;
        System.out.println(Thread.currentThread().getName() + ":" + count);
        notify();
    }

    public synchronized void decrease() {
        if (count == 0) {
            try {
                //等待，由于Decrease线程调用的该方法,
                //所以Decrease线程进入对象t(main函数中实例化的)的等待池，并且释放对象t的锁
                wait();//Object类的方法
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        System.out.println(Thread.currentThread().getName() + ":" + count);

        //唤醒线程Increase，Increase线程从等待池到锁池
        notify();
    }
}
